package top.revery.admin.controller_admin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.revery.admin.service.ChinaCityDataSource;
import top.revery.admin.util.FeignServiceMapping;
import top.revery.api.config.HttpResponResult;
import top.revery.api.config.ResultType;
import top.revery.api.entity.other.BaseChinaCity;

import java.io.File;

/**
 * <h1> ---  --- </h1>
 * Created by  revery.top jun
 * Date  2019-12-11 19:31
 */
@RestController
@RequestMapping("/admin/china_city")
public class ChinaCityController {
    private ChinaCityDataSource chinaCityDataSource = FeignServiceMapping.chinaCityDataSource;

    @RequestMapping("/getList")
    HttpResponResult getList(@RequestParam(defaultValue = "0") int pid){
        HttpResponResult httpResponResult = new HttpResponResult(ResultType.SUCCESS);
        httpResponResult.put("List",chinaCityDataSource.getList(pid));
        return httpResponResult;
    }

    @RequestMapping("/init")
    Object init() throws Exception {
        File file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX+"config/china.json");
        JSONObject json = JSON.parseObject(FileUtils.readFileToString(file));

        JSONObject provinceObj = json.getJSONObject("0");

        for(String key : provinceObj.keySet()){
            System.out.println(key+"  =  "+provinceObj.get(key));
            BaseChinaCity baseChinaCity = new BaseChinaCity();
            baseChinaCity.setCode(key);
            baseChinaCity.setName(provinceObj.getString(key));
            baseChinaCity = chinaCityDataSource.save(baseChinaCity);

            JSONObject cityObj = json.getJSONObject("0,"+key);
            for(String cityKey : cityObj.keySet()){
                System.out.println("-----    "+cityKey+"  =  "+cityObj.get(cityKey));

                BaseChinaCity baseChinaCity2 = new BaseChinaCity();
                baseChinaCity2.setCode(cityKey);
                baseChinaCity2.setName(cityObj.getString(cityKey));
                baseChinaCity2.setPid(Integer.valueOf(baseChinaCity.getId()+""));
                baseChinaCity2 = chinaCityDataSource.save(baseChinaCity2);

                if(!(json.get("0,"+key+","+cityKey) instanceof JSONArray)){
                    JSONObject areObj = json.getJSONObject("0,"+key+","+cityKey);
                    for(String areKey : areObj.keySet()){
                        System.out.println("-----    -----    "+areKey+"  =  "+areObj.get(areKey));

                        BaseChinaCity baseChinaCity3 = new BaseChinaCity();
                        baseChinaCity3.setCode(areKey);
                        baseChinaCity3.setName(areObj.getString(areKey));
                        baseChinaCity3.setPid(Integer.valueOf(baseChinaCity2.getId()+""));
                        baseChinaCity3 = chinaCityDataSource.save(baseChinaCity3);
                    }
                }

            }

        }
        return null;
    }
}
